package com.baichang.taoshunda.service.impl;

import com.baichang.taoshunda.dao.GoodsSpecDao;
import com.baichang.taoshunda.dao.GoodsSpecValueDao;
import com.baichang.taoshunda.entity.GoodsSpec;
import com.baichang.taoshunda.entity.GoodsSpecValue;
//import com.baichang.taoshunda.service.DiscountActivityService;
import com.baichang.taoshunda.service.GoodsSpecService;
import com.baichang.taoshunda.utils.ConvertTypeUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class GoodsSpecServiceImpl implements GoodsSpecService {

    @Autowired
    private GoodsSpecDao goodsSpecDao;

    @Autowired
    private GoodsSpecValueDao goodsSpecValueDao;
//    @Autowired
//    private DiscountActivityService discountActivityService;

    @Override
    public List<Map<String, Object>> findGoodsSpecByGoodsId(Integer goodsId) {
        List<Map<String, Object>> returnMapList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> specs = goodsSpecDao.findGoodsSpec(goodsId);
        if (specs != null && specs.size() > 0) {
            for (Map<String, Object> spec : specs) {
                Integer specId = ConvertTypeUtils.obj2Integer(spec.get("id"));
                List<Map<String, Object>> values = goodsSpecValueDao.findGoodsSpecValues(specId);
                Map<String, Object> newMap = spec;
                newMap.put("values", values);
                returnMapList.add(newMap);
            }
        }
        return returnMapList;
    }

    /**
     * 查看商品的规格信息、折扣活动
     *
     * @param goodsId
     */
    @Override
    public List<Map<String, Object>> findTakeoutGoodsSpecByGoodsId(Integer goodsId) {
        List<Map<String, Object>> returnMapList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> specs = goodsSpecDao.findGoodsSpec(goodsId);
//        List<Map<String, Object>> discountActivitys = discountActivityService.findDiscountListByGoodsId(goodsId);
        if (specs != null && specs.size() > 0) {
            for (Map<String, Object> spec : specs) {
                Integer specId = ConvertTypeUtils.obj2Integer(spec.get("id"));
                List<Map<String, Object>> values = goodsSpecValueDao.findGoodsSpecValues(specId);
                Map<String, Object> newMap = spec;
                newMap.put("values", values);
                returnMapList.add(newMap);
            }
        }
//        if (discountActivitys != null && discountActivitys.size() > 0) {
//            for (Map<String, Object> discountActivity : discountActivitys) {
//                Map<String, Object> newMap = discountActivity;
//                newMap.put("discountActivity", discountActivity);
//                returnMapList.add(newMap);
//            }
//        }
        return returnMapList;
    }

    /**
     * 添加规格属性
     *
     * @param isPrice
     * @param spec
     * @param companyId
     * @param goodsId
     * @param isImage
     * @return
     */
    @Override
    public boolean addSpec(Integer isPrice, String spec, Integer companyId, Integer goodsId, Integer isImage) {
        boolean flag = false;
        JSONArray jsonArray = JSONArray.fromObject(spec);
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = JSONObject.fromObject(jsonArray.get(i));
                GoodsSpec goodsSpec = new GoodsSpec();
                goodsSpec.setCompanyId(companyId);
                goodsSpec.setCreated(new Date());
                goodsSpec.setIsPrice(isPrice);
                goodsSpec.setIsImages(isImage);
                goodsSpec.setGoodsId(goodsId);
                goodsSpec.setSpecName(jsonObject.get("specName").toString());
                long id = goodsSpecDao.save(goodsSpec);
                JSONArray specValueArray = JSONArray.fromObject(jsonObject.get("specValue").toString());
                for (int j = 0; j < specValueArray.size(); j++) {
                    JSONObject specValue = JSONObject.fromObject(specValueArray.get(j));
                    GoodsSpecValue goodsSpecValue = new GoodsSpecValue();
                    goodsSpecValue.setCreated(new Date());
                    goodsSpecValue.setPrice(BigDecimal.valueOf(Double.parseDouble(specValue.get("price").toString())));
                    goodsSpecValue.setSpecId((int) id);
                    goodsSpecValue.setValueName(specValue.get("value").toString());
                    goodsSpecValue.setImage(specValue.get("images").toString());
                    flag = goodsSpecValueDao.save(goodsSpecValue) > 0;
                }
            }
        } else {
            flag = true;
        }
        return flag;
    }

    /**
     * 修改规格
     *  @param isPrice
     * @param companyId
     * @param spec
     * @param goodsId
     * @param isImage
     */
    @Override
    public boolean updateSpecValue(Integer isPrice, Integer companyId, String spec, Integer goodsId, Integer isImage) {
        return addSpec(isPrice, spec, companyId, goodsId, isImage);
    }

    /**
     * 删除某一个规格
     *
     * @param specId
     * @return
     */
    @Override
    public boolean deleteBySpecId(Integer specId) {
        if (goodsSpecValueDao.deleteBySpecValueId(specId)) {
            return goodsSpecDao.deleteById(specId) > 0;
        }
        return false;
    }

    @Override
    public void deleteByGoodsId(Integer id) {
        deleteSpecValue(1, id);
        deleteSpecValue(0, id);
    }

    /**
     * 删除规格
     *
     * @param isPrice
     * @param goodsId
     * @return
     */
    private boolean deleteSpecValue(Integer isPrice, Integer goodsId) {
        if (goodsSpecValueDao.deleteBySpecId(isPrice, goodsId)) {
            return goodsSpecDao.deleteByGoodsId(isPrice, goodsId);
        }
        return false;
    }

}
